<!-- 活动商品卡片中的按钮 -->
<div class="promotion-actions">
    <button class="buy-now" onclick="buyPromotion(${promotion.productId}, '${promotion.productName}', ${promotion.promotionPrice}, '${promotion.productImage}')">
        立即抢购
    </button>
</div>

<script>
    // 活动抢购功能
    function buyPromotion(productId, productName, promotionPrice, image) {
        // 检查是否登录
        <% if (session.getAttribute("userId") == null) { %>
            if (confirm('请先登录后再购买，是否前往登录？')) {
                window.location.href = 'login.jsp';
            }
            return;
        <% } %>
        
        // 创建一个包含单个活动商品的数组
        const items = [{
            productId: productId,
            quantity: 1,
            price: promotionPrice,
            subtotal: promotionPrice,
            name: productName,
            image: image
        }];
        
        // 将商品信息转换为查询字符串
        const itemsParam = encodeURIComponent(JSON.stringify(items));
        
        // 构建支付页面URL
        const paymentUrl = new URL('payment.jsp', window.location.href);
        paymentUrl.searchParams.set('amount', promotionPrice.toFixed(2));
        paymentUrl.searchParams.set('discount', '0.00'); // 活动价格已经是优惠价，不再额外优惠
        paymentUrl.searchParams.set('items', itemsParam);
        paymentUrl.searchParams.set('itemCount', '1');
        
        // 跳转到支付页面
        window.location.href = paymentUrl.toString();
    }
</script> 